package cn.ronghuanet.auth.service.impl;

import cn.ronghuanet.auth.dto.LoginDTO;
import cn.ronghuanet.auth.service.ILoginService;
import cn.ronghuanet.base.util.LoginContext;
import cn.ronghuanet.org.domain.Employee;
import cn.ronghuanet.org.mapper.EmployeeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;

@Service
public class LoginServiceImpl implements ILoginService {
    @Autowired
    private EmployeeMapper mapper;
    @Override
    public Map<String,Object> login(LoginDTO user) {
        /*
            登录的两种判断方式 我们选择 账号先查对象 然后再匹配密码
         */
        if (StringUtils.isEmpty(user.getUsername())||StringUtils.isEmpty(user.getPassword())){
            throw new RuntimeException("请输入账号和密码");
        }
        // 根据账号查对象
        Employee employee1 = mapper.selectByName(user.getUsername());
        if (Objects.isNull(employee1)){
            throw new RuntimeException("账号或密码错误");
        }
        // 匹配密码
        if (!user.getPassword().equals(employee1.getPassword())){
            throw new RuntimeException("账号或密码错误");
        }
        // 将user存入map
        String uuid = UUID.randomUUID().toString();
        LoginContext.loginMap.put(uuid,employee1);
        Map<String, Object> map = new HashMap<>();
        map.put("token",uuid); //token叫做令牌 就是一个唯一的标识符而已
        employee1.setPassword(null);
//        employee1.setId(null);
        map.put("user",employee1); //token叫做令牌 就是一个唯一的标识符而已
        return map;
    }
}
